package com.kool.kreport.service;

import com.kool.kreport.bean.*;
import com.kool.kreport.constant.DateDimEnum;
import com.kool.kreport.dao.*;
import com.kool.kreport.request.GetGlobalDailyReportReq;
import com.kool.kreport.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author luyu
 * @date 2021-04-19 23:23
 */
@Service
@Slf4j
public class QueryService {
    @Resource
    private ReportGlobalPacketCountDao globalPacketCountDao;
    @Resource
    private ReportGlobalSessionCountDao globalSessionCountDao;
    @Resource
    private ReportGlobalNoAckCountDao globalNoAckCountDao;
    @Resource
    private ReportGlobalTopicPublishCountDao globalTopicPublishCountDao;
    @Resource
    private ReportGlobalSubscribeCountDao globalSubscribeCountDao;
    @Resource
    private ReportUserPacketCountDao userPacketCountDao;
    @Resource
    private ReportClientPacketCountDao clientPacketCountDao;
    @Resource
    private ReportAddressPacketCountDao addressPacketCountDao;

    /**
     * 查询平台日报表
     *
     * @param request
     * @return
     */
    public List<DailyReportVo> getGlobalDailyReport(GetGlobalDailyReportReq request) {
        int from = request.getFromDay();
        int to = request.getToDay();

        Map<Integer, DailyReportVo> dailyReportVoMap = new HashMap<>();
        List<ReportGlobalPacketCountDo> globalPacketCountReports = globalPacketCountDao.sumGroupByDay(from, to);
        if (globalPacketCountReports != null) {
            for (ReportGlobalPacketCountDo globalPacketCountDo : globalPacketCountReports) {
                int day = globalPacketCountDo.getReportDate();
                DailyReportVo dailyReportVo = dailyReportVoMap.getOrDefault(day, new DailyReportVo());
                dailyReportVo.setDay(day)
                        .setReceiveSuccess(globalPacketCountDo.getReceiveSuccess())
                        .setReceiveTotal(globalPacketCountDo.getReceiveTotal())
                        .setSendSuccess(globalPacketCountDo.getSendSuccess())
                        .setSendTotal(globalPacketCountDo.getSendTotal());
                dailyReportVoMap.put(day, dailyReportVo);
            }
        }
        List<ReportGlobalSessionCountDo> globalSessionCountReports = globalSessionCountDao.getByDateRange(DateDimEnum.Day.getCode(), from, to);
        if (globalSessionCountReports != null) {
            for (ReportGlobalSessionCountDo globalSessionCountDo : globalSessionCountReports) {
                int day = globalSessionCountDo.getReportDate();
                DailyReportVo dailyReportVo = dailyReportVoMap.getOrDefault(day, new DailyReportVo());
                dailyReportVo.setDay(day)
                        .setAvgSessionCnt(globalSessionCountDo.getAvgCnt())
                        .setMaxSessionCnt(globalSessionCountDo.getMaxCnt());
                dailyReportVoMap.put(day, dailyReportVo);
            }
        }
        List<ReportGlobalNoAckCountDo> globalNoAckCountReports = globalNoAckCountDao.getByDateRange(DateDimEnum.Day.getCode(), from, to);
        if (globalNoAckCountReports != null) {
            for (ReportGlobalNoAckCountDo globalNoAckCountDo : globalNoAckCountReports) {
                int day = globalNoAckCountDo.getReportDate();
                DailyReportVo dailyReportVo = dailyReportVoMap.getOrDefault(day, new DailyReportVo());
                dailyReportVo.setDay(day)
                        .setNoAckCnt(globalNoAckCountDo.getMaxCnt());
                dailyReportVoMap.put(day, dailyReportVo);
            }
        }

        return new ArrayList<>(dailyReportVoMap.values());
    }

    /**
     * 查询主题top报表
     *
     * @param day
     * @param top
     * @return
     */
    public TopicTopReport getTopicTopDailyReport(int day, int top) {
        TopicTopReport topicTopReport = new TopicTopReport();
        List<ReportGlobalTopicPublishCountDo> topicReceiveSuccessPublishCntTop = globalTopicPublishCountDao.getReceiveSuccessTop(day, top);
        if (topicReceiveSuccessPublishCntTop != null) {
            topicTopReport.setTopicReceiveSuccessPublishCntTop(topicReceiveSuccessPublishCntTop.stream().map(item -> {
                TopicCnt topicCnt = new TopicCnt();
                topicCnt.setTopicName(item.getTopicName());
                topicCnt.setCnt(item.getReceiveSuccess());
                return topicCnt;
            }).collect(Collectors.toList()));
        }
        List<ReportGlobalTopicPublishCountDo> topicSendSuccessPublishCntTop = globalTopicPublishCountDao.getSendSuccessTop(day, top);
        if (topicSendSuccessPublishCntTop != null) {
            topicTopReport.setTopicSendSuccessPublishCntTop(topicSendSuccessPublishCntTop.stream().map(item -> {
                TopicCnt topicCnt = new TopicCnt();
                topicCnt.setTopicName(item.getTopicName());
                topicCnt.setCnt(item.getSendSuccess());
                return topicCnt;
            }).collect(Collectors.toList()));
        }
        List<ReportGlobalSubscribeCountDo> subscribeCntTop = globalSubscribeCountDao.getClientCntTop(day, top);
        if (subscribeCntTop != null) {
            topicTopReport.setTopicFilterSubscriberCntTop(subscribeCntTop.stream().map(item -> {
                TopicFilterCnt topicFilterCnt = new TopicFilterCnt();
                topicFilterCnt.setTopicFilter(item.getTopicFilter());
                topicFilterCnt.setCnt(item.getClientCnt());
                return topicFilterCnt;
            }).collect(Collectors.toList()));
        }
        return topicTopReport;
    }

    /**
     * 查询用户top报表
     *
     * @param day
     * @param top
     * @return
     */
    public UserTopReport getUserTopDailyReport(int day, int top) {
        UserTopReport userTopReport = new UserTopReport();
        List<ReportUserPacketCountDo> userReceiveSuccessCntTop = userPacketCountDao.getReceiveSuccessTop(day, top);
        if (userReceiveSuccessCntTop != null) {
            userTopReport.setUserReceiveSuccessCntTop(userReceiveSuccessCntTop.stream().map(item -> {
                UserCnt userCnt = new UserCnt();
                userCnt.setUserName(item.getUserName());
                userCnt.setCnt(item.getReceiveSuccess());
                return userCnt;
            }).collect(Collectors.toList()));
        }
        List<ReportUserPacketCountDo> userSendSuccessCntTop = userPacketCountDao.getSendSuccessTop(day, top);
        if (userSendSuccessCntTop != null) {
            userTopReport.setUserSendSuccessCntTop(userSendSuccessCntTop.stream().map(item -> {
                UserCnt userCnt = new UserCnt();
                userCnt.setUserName(item.getUserName());
                userCnt.setCnt(item.getSendSuccess());
                return userCnt;
            }).collect(Collectors.toList()));
        }
        return userTopReport;
    }

    /**
     * 查询客户端top报表
     *
     * @param day
     * @param top
     * @return
     */
    public ClientTopReport getClientTopDailyReport(int day, int top) {
        ClientTopReport clientTopReport = new ClientTopReport();
        List<ReportClientPacketCountDo> clientReceiveSuccessCntTop = clientPacketCountDao.getReceiveSuccessTop(day, top);
        if (clientReceiveSuccessCntTop != null) {
            clientTopReport.setClientReceiveSuccessCntTop(clientReceiveSuccessCntTop.stream().map(item -> {
                ClientIdCnt clientIdCnt = new ClientIdCnt();
                clientIdCnt.setClientId(item.getClientId());
                clientIdCnt.setCnt(item.getReceiveSuccess());
                return clientIdCnt;
            }).collect(Collectors.toList()));
        }
        List<ReportClientPacketCountDo> clientSendSuccessCntTop = clientPacketCountDao.getSendSuccessTop(day, top);
        if (clientSendSuccessCntTop != null) {
            clientTopReport.setClientSendSuccessCntTop(clientSendSuccessCntTop.stream().map(item -> {
                ClientIdCnt clientIdCnt = new ClientIdCnt();
                clientIdCnt.setClientId(item.getClientId());
                clientIdCnt.setCnt(item.getSendSuccess());
                return clientIdCnt;
            }).collect(Collectors.toList()));
        }
        List<ReportAddressPacketCountDo> addressReceiveSuccessCntTop = addressPacketCountDao.getReceiveSuccessTop(day, top);
        if (addressReceiveSuccessCntTop != null) {
            clientTopReport.setRemoteAddressReceiveSuccessCntTop(addressReceiveSuccessCntTop.stream().map(item -> {
                RemoteAddressCnt addressCnt = new RemoteAddressCnt();
                addressCnt.setRemoteAddress(item.getRemoteAddress());
                addressCnt.setCnt(item.getReceiveSuccess());
                return addressCnt;
            }).collect(Collectors.toList()));
        }
        List<ReportAddressPacketCountDo> addressSendSuccessCntTop = addressPacketCountDao.getSendSuccessTop(day, top);
        if (addressSendSuccessCntTop != null) {
            clientTopReport.setRemoteAddressSendSuccessCntTop(addressSendSuccessCntTop.stream().map(item -> {
                RemoteAddressCnt addressCnt = new RemoteAddressCnt();
                addressCnt.setRemoteAddress(item.getRemoteAddress());
                addressCnt.setCnt(item.getSendSuccess());
                return addressCnt;
            }).collect(Collectors.toList()));
        }
        return clientTopReport;
    }
}
